package org.summer.lawyer.handle;

import cn.hutool.core.util.ObjectUtil;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.summer.common.json.utils.JsonUtils;
import org.summer.lawyer.constant.SmsConstants;

import java.util.LinkedHashMap;

/**
 *
 */
@Service
public class SmsService {

    private static final Logger log = LoggerFactory.getLogger(SmsService.class);

    /**
     * 用户订单通知
     * 尊敬的${name}，你的需求我们已经收到小咕正在帮您匹配律师。
     */
    public void userOrderSms(String phoneNumber, String name) {
        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
        map.put("name", name);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, SmsConstants.SMS_469015588, map);
        if(!smsResponse.isSuccess()){
            log.error("[用户订单通知]短信发送失败，错误信息：{}", JsonUtils.toJsonString(smsResponse));
        }
    }

    /**
     * 用户文书模版
     * 尊敬的${name}，你下单的${filename}文书已发送成功，请及时查看邮箱。
     */
    public void userFileOrderSms(String phoneNumber, String name, String spuName) {
        log.info("[userFileOrderSms][用户文书模版], phoneNumber={}, name={}", phoneNumber, name);
        LinkedHashMap<String, String> map = new LinkedHashMap<>(2);
        map.put("name", name);
        map.put("filename", spuName);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        if(ObjectUtil.isNull(smsBlend)){
            log.error("[userFileOrderSms][用户文书模版] smsBlend is null");
        }
        SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, SmsConstants.SMS_468935600, map);
        if(!smsResponse.isSuccess()){
            log.error("[用户文书模版]短信发送失败，错误信息：{}", JsonUtils.toJsonString(smsResponse));
        }
    }

    /**
     * 用户分配律师
     * 尊敬的${name}，你提交的${order}已经由${companyName}律师接收，${lsName}律师将在第一时间会与您联系，请保持电话畅通。
     */
    public void assignLawFirmSms(String phoneNumber, String name, String spuName, String companyName, String lsName) {
        LinkedHashMap<String, String> map = new LinkedHashMap<>(4);
        map.put("name", name);
        map.put("order", spuName);
        map.put("companyName", companyName);
        map.put("lsName", lsName);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, SmsConstants.SMS_468970644, map);
        if(!smsResponse.isSuccess()){
            log.error("[用户分配律师]短信发送失败，错误信息：{}", JsonUtils.toJsonString(smsResponse));
        }
    }

    /**
     * 用户重新分配通知
     * 尊敬的${name}，由于${companyName}律师没有及时回应，我们已经重新安排${lsName}律师接收你的服务，律师会尽快与您联系，敬请谅解。
     */
    public void reallocateLawFirmSms(String phoneNumber, String name,  String companyName, String lsName) {
        LinkedHashMap<String, String> map = new LinkedHashMap<>(3);
        map.put("name", name);
        map.put("companyName", companyName);
        map.put("lsName", lsName);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, SmsConstants.SMS_468855824, map);
        if(!smsResponse.isSuccess()){
            log.error("[用户重新分配通知]短信发送失败，错误信息：{}", JsonUtils.toJsonString(smsResponse));
        }
    }

    /**
     * 律师订单通知
     * 尊敬的${companyName}律师，您有新订单，客户${username}在咕法网平台上购买了${order}服务，请进入商家页面查看订单详情。
     */
    public void lawFirmOrderSms(String phoneNumber, String name, String spuName, String companyName) {
        LinkedHashMap<String, String> map = new LinkedHashMap<>(3);
        map.put("companyName", companyName);
        map.put("username", name);
        map.put("order", spuName);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, SmsConstants.SMS_468905646, map);
        if(!smsResponse.isSuccess()){
            log.error("[律师订单通知]短信发送失败，错误信息：{}", JsonUtils.toJsonString(smsResponse));
        }
    }

    /**
     * 用户订单完成
     * 尊敬的${name}，你的订单已完成，请点击此处完成对${companyName}律师的评价，您的建议对我们非常重要。拒收请回复R
     */
    public void completedOrderSms(String phoneNumber, String name,  String companyName) {
        log.info("[completedOrderSms][用户订单完成短信], phoneNumber={}, name={}, companyName={}", phoneNumber, name, companyName);
        LinkedHashMap<String, String> map = new LinkedHashMap<>(2);
        map.put("name", name);
        map.put("companyName", companyName);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, SmsConstants.SMS_469095666, map);
        if(!smsResponse.isSuccess()){
            log.error("[律师订单通知]短信发送失败，错误信息：{}", JsonUtils.toJsonString(smsResponse));
        }
    }

    /**
     *  律师超时通知
     *  尊敬的${lsName}律师，由于您没有积极响应查看客户${name}在订单详情，该订单已失效。
     */
    public void orderReturnedSms(String phoneNumber, String lsName,  String name) {
        LinkedHashMap<String, String> map = new LinkedHashMap<>(2);
        map.put("lsName", lsName);
        map.put("name", name);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, SmsConstants.SMS_468890889, map);
        if(!smsResponse.isSuccess()){
            log.error("[律师超时通知]短信发送失败，错误信息：{}", JsonUtils.toJsonString(smsResponse));
        }
    }
}
